﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Controls;
using UniMana.Data;
using UniMana.Utils.Data;
namespace UniMana.Win.Training
{
    public partial class FrmScore_TaskInput : DevExpress.XtraEditors.XtraForm
    {
        private DBMain m_Score = new DBMain();
        private DBMain m_dbTraining = new DBMain();
        private bds m_bds = new bds();
        private DBMain m_Catalogue = new DBMain();
        private bds m_bds1 = new bds();
        private int m_iScoreTypeKey = -1;
        private bool m_bCanAdd = false;
        private bool m_bInput;
        private List<int> m_ICassKey;
        public FrmScore_TaskInput(List<int> _iLClassKey, bool _bInput = true)
        {
            InitializeComponent();
            UniMana.Data.SysUtils.SetRuleControl(this.Controls, this.Name);

            this.m_bInput = _bInput;
            if (!this.m_bInput)
            {
                this.Name = "FrmScore_TaskCheck";
                this.Text = "Phân công kiểm tra nhập điểm";
            }
            
            ShowClassInfo(_iLClassKey);
            this.m_ICassKey = _iLClassKey;
            GetPersonelAndTask();
        }
        public FrmScore_TaskInput()
        {
            InitializeComponent();
            UniMana.Data.SysUtils.SetRuleControl(this.Controls, this.Name);
        }
        public void Load(List<int> _iLClassKey, bool _bInput = true)
        {
            this.m_bInput = _bInput;
            if (!this.m_bInput)
            {
                this.Name = "FrmScore_TaskCheck";
                this.Text = "Phân công kiểm tra nhập điểm";
            }
            ShowClassInfo(_iLClassKey);
            this.m_ICassKey = _iLClassKey;
            GetPersonelAndTask();
        }

        private void ShowClassInfo(List<int> _iLClassKey)
        {
            this.gctTraining_Subject_Class.DataSource = this.m_dbTraining.m_DCReadOnly.Training_Subject_Classes.GetListSubjectClass(_iLClassKey).ToDataTable();
        }

        private void GetPersonelAndTask()
        {
            if (this.m_ICassKey.Count > 1)
            {
                DateTime dteNow = UniMana.Data.SysDateTime.Now;
                this.gctPersonnel_Personnel.DataSource = this.m_dbTraining.m_DCReadOnly.Personnel_Personnels.GetListNotTaskInputScore(this.m_dbTraining.m_DCReadOnly.Training_Score_TaskInputs,this.m_dbTraining.m_DCReadOnly.System_Users,this.m_dbTraining.m_DCReadOnly.System_User_Rules,this.m_dbTraining.m_DCReadOnly.System_Rules, this.m_ICassKey,dteNow,Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey, this.m_bInput);

                this.m_bds = new bds();
                this.m_bds.DataSource = this.m_dbTraining.m_DCReadOnly.Training_Score_TaskInputs.GetByTrainingClassKey(this.m_dbTraining.m_DCReadOnly.Personnel_Personnels, this.m_ICassKey,dteNow, this.m_bInput);
                this.rluPersonnel_PersonnelID.DataSource = this.m_bds;
                this.rluPersonnel_PersonnelLastName.DataSource = this.m_bds;
                this.rluPersonnel_PersonnelName.DataSource = this.m_bds;

                ///this.m_bds1.DataSource = this.m_dbTraining.m_DCSave.Training_Score_TaskInputs.GetListByClassKey(this.m_ICassKey, this.m_bInput);
                //this.gctTraining_Score_TaskInput.DataSource = this.m_bds1;
                DataTable dt = this.m_dbTraining.m_DCSave.Training_Score_TaskInputs.GetListByClassKey(this.m_ICassKey,dteNow, this.m_bInput).ToDataTable();
                if (dt.Columns.Count == 0)
                {
                    dt.Columns.Add("Training_Subject_ClassKey",typeof(int));
                    dt.Columns.Add("Personnel_PersonnelKey", typeof(Guid));
                    dt.Columns.Add("Date", typeof(DateTime));
                    dt.Columns.Add("Period", typeof(int));
                    dt.Columns.Add("Note", typeof(int));
                    dt.Columns.Add("Input",typeof(bool));
                }
                this.gctTraining_Score_TaskInput.DataSource = dt;// this.m_dbTraining.m_DCSave.Training_Score_TaskInputs.GetListByClassKey(this.m_ICassKey, this.m_bInput).ToDataTable();
            }
            else
            {
                DateTime dteNow = UniMana.Data.SysDateTime.Now;
                int iTraining_Subject_ClassKey = this.m_ICassKey[0];
                this.gctPersonnel_Personnel.DataSource = this.m_dbTraining.m_DCReadOnly.Personnel_Personnels.GetListNotTaskInputScore(this.m_dbTraining.m_DCReadOnly.Training_Score_TaskInputs, this.m_dbTraining.m_DCReadOnly.System_Users, this.m_dbTraining.m_DCReadOnly.System_User_Rules, this.m_dbTraining.m_DCReadOnly.System_Rules, iTraining_Subject_ClassKey, Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey, this.m_bInput);

                this.m_bds = new bds();
                this.m_bds.DataSource = this.m_dbTraining.m_DCReadOnly.Training_Score_TaskInputs.GetByTrainingClassKey(this.m_dbTraining.m_DCReadOnly.Personnel_Personnels, iTraining_Subject_ClassKey, this.m_bInput);
                this.rluPersonnel_PersonnelID.DataSource = this.m_bds;
                this.rluPersonnel_PersonnelLastName.DataSource = this.m_bds;
                this.rluPersonnel_PersonnelName.DataSource = this.m_bds;

                this.m_bds1.DataSource = this.m_dbTraining.m_DCSave.Training_Score_TaskInputs.GetListByClassKey(iTraining_Subject_ClassKey,dteNow, this.m_bInput);
                this.gctTraining_Score_TaskInput.DataSource = this.m_bds1;
            }
            //this.gctTraining_Score_TaskInput.DataSource = 
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (!UniMana.Data.SysUtils.CheckEditByRule(Utils.Configs.RuleConfig.SCORETASKINPUT))
            {
                XtraMessageBox.Show("Bạn không có quyền này");
                return;
            }
            if (this.grvPersonnel_Personnel.RowCount > 0)
            {
                if (this.grvPersonnel_Personnel.RowCount > 0)
                {
                    if (this.m_bCanAdd || true) //Test lai
                    {
                        DateTime now = UniMana.Data.SysDateTime.Now;
                        if (!this.m_bInput)
                        {
                            DataTable dt = this.m_dbTraining.m_DCReadOnly.Training_Score_TaskInputs.CheckAddInput(this.m_dbTraining.m_DCReadOnly.Training_Subject_Classes, this.m_ICassKey, now).ToDataTable();
                            if (dt.Rows.Count > 0)
                            {
                                string strMessbox = "";
                                for (int i = 0; i < dt.Rows.Count; i++)
                                {
                                    strMessbox += dt.Rows[i]["Name"].ToString() + "\n";
                                }
                                if (DialogResult.No == XtraMessageBox.Show("Các lớp: " + strMessbox + " Chưa phân công nhập điểm nên không thể phân công kiểm tra điểm. \r\nHoặc đã nhập điểm xong.\r\nBạn có chắc là muốn thêm vào.", "Xác nhận", MessageBoxButtons.YesNo))
                                {
                                    return;
                                }
                            }
                        }
                        for (int i = 0; i < this.m_ICassKey.Count; i++)
                        {
                            int[] rows = this.grvPersonnel_Personnel.GetSelectedRows();
                            int iTraining_Subject_ClassKey = this.m_ICassKey[i];

                            for (int j = 0; j < rows.Length; j++)
                            {
                                Guid personelKey = this.grvPersonnel_Personnel.GetRowCellValue(rows[j], gclPersonnel_PersonnelKeyLeft).ToGuid();
                                Training_Score_TaskInputInfo taskInfo = new Training_Score_TaskInputInfo(personelKey, iTraining_Subject_ClassKey,now,this.m_bInput);
                                if (!taskInfo.ExistInDataBase)
                                {
                                    taskInfo.Properties.Personnel_PersonnelKey = personelKey;
                                    taskInfo.Properties.Training_Subject_ClassKey = iTraining_Subject_ClassKey;
                                    taskInfo.Properties.Input = this.m_bInput;
                                    taskInfo.Properties.Date = UniMana.Data.SysDateTime.Now;
                                    taskInfo.Properties.Period = 7; //Tạm thời mặc định
                                    taskInfo.Properties.Lock = false;
                                    taskInfo.Properties.Personnel_PersonnelKeyLeader = UniMana.Utils.LoginInfo.GblLoginInfo.sysLoginPersionalKey;
                                    taskInfo.Save();
                                }
                            }
                        }
                        this.grvPersonnel_Personnel.DeleteSelectedRows();
                        GetPersonelAndTask();
                    }
                    else
                    {
                        XtraMessageBox.Show("Chưa thể phân công cho cột điểm này");
                    }
                }
                else
                {
                    XtraMessageBox.Show("Chưa chọn nhân viên.");
                }
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            if (!UniMana.Data.SysUtils.CheckEditByRule(Utils.Configs.RuleConfig.SCORETASKINPUT))
            {
                XtraMessageBox.Show("Bạn không có quyền này");
                return;
            }
            if (this.grvTraining_Score_TaskInput.RowCount > 0)
            {
                if (this.grvTraining_Score_TaskInput.RowCount > 0)
                {
                    if (this.m_ICassKey.Count == 1)
                    {
                        int[] rows = this.grvTraining_Score_TaskInput.GetSelectedRows();
                        for (int i = 0; i < rows.Length; i++)
                        {
                            int iTraining_Score_TaskInputKey = this.grvTraining_Score_TaskInput.GetRowCellValue(rows[i], gclTraining_Score_TaskInputKey).ToInt();
                            Training_Score_TaskInputInfo taskInfo = new Training_Score_TaskInputInfo(iTraining_Score_TaskInputKey);
                            if (taskInfo.ExistInDataBase)
                            {
                                if (taskInfo.Properties.Lock != null)
                                {
                                    if (taskInfo.Properties.Lock.Value == false)
                                    {
                                        taskInfo.Delete();
                                        taskInfo.Save();
                                        GetPersonelAndTask();
                                    }
                                    else
                                    {
                                        XtraMessageBox.Show("Không thể xóa dữ liệu này.");
                                    }
                                }
                                else
                                {
                                    taskInfo.Delete();
                                    taskInfo.Save();
                                    GetPersonelAndTask();
                                }
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < this.m_ICassKey.Count; i++)
                        {
                            int classKey = this.m_ICassKey[i];
                            int[] rows = this.grvTraining_Score_TaskInput.GetSelectedRows();
                            for (int j = 0; j < rows.Length; j++)
                            {
                                Guid personelKey = this.grvTraining_Score_TaskInput.GetRowCellValue(rows[j], gclPersonnel_PersonnelKeyRight).ToGuid();
                                DateTime now = UniMana.Data.SysDateTime.Now;
                                Training_Score_TaskInputInfo taskInfo = new Training_Score_TaskInputInfo(personelKey, classKey, now, this.m_bInput);
                                taskInfo.Delete();
                                taskInfo.Save();
                            }
                        }
                        GetPersonelAndTask();
                    }
                }
                else
                {
                    XtraMessageBox.Show("Chưa chọn nhân viên.");
                }
            }
        }

        private void grvTraining_Score_TaskInput_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (this.m_ICassKey.Count == 1)
            {
                this.Validate();
                this.m_bds1.EndEdit();
                this.m_dbTraining.m_DCSave.SubmitChanges();
            }
            else
            {
                for (int i = 0; i < this.m_ICassKey.Count; i++)
                {
                    int classKey = this.m_ICassKey[i];
                    Guid personelKey = this.grvTraining_Score_TaskInput.GetFocusedRowCellValue(gclPersonnel_PersonnelKeyRight).ToGuid();
                    DateTime now = UniMana.Data.SysDateTime.Now;
                    Training_Score_TaskInputInfo taskInfo = new Training_Score_TaskInputInfo(personelKey, classKey, now, this.m_bInput);
                    if (e.Column == gclDate)
                    {
                        taskInfo.Properties.Date = e.Value.ToDateTime();
                    }
                    else if (e.Column == gclPeriod)
                    {
                        taskInfo.Properties.Period = e.Value.ToInt();
                    }
                    else
                    {
                        taskInfo.Properties.Note = e.Value.ToString();
                    }
                    taskInfo.Save();
                }
            }
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            
        }

        private void groupControl1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void xemThôngTinNhânViênToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Guid personelKey;
                if (gctPersonnel_Personnel.Focused)
                {
                    personelKey = Guid.Parse(grvPersonnel_Personnel.GetFocusedRowCellValue(gclPersonnel_PersonnelKeyLeft).ToString());
                }
                else
                {
                    personelKey = Guid.Parse(grvTraining_Score_TaskInput.GetFocusedRowCellValue(gclPersonnel_PersonnelKeyRight).ToString());
                }
                //|EDIT|
                //UniMana.GUI.Personnel.FrmPersonelInfo f = new UniMana.GUI.Personnel.FrmPersonelInfo();
                //UniMana.Lib.Personel.Personnel_PersonnelInfo info = new UniMana.Lib.Personel.Personnel_PersonnelInfo(personelKey);
                //f.m_personelInfo = info;
                //f.m_Flag = false;
                //f.ShowDialog();
            }
            catch { }
        }

        //private void txtSubjectClassID_EditValueChanged(object sender, EventArgs e)

    }
}